package com.testdemo.common.view;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.testdemo.R;
import com.testdemo.common.utils.CommonUtil;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by xuchichi on 2017/3/24.
 * 公共弹框
 */
public class ItemsPopupWindow extends PopupWindow {
    int selectPosition = 0;
    Context mContext;
    ListView listview;
    ArrayList<String> arrayList;
    AdapterView.OnItemClickListener onItemClickListener;
    PopAdapter adapter;
    View view;

    public void setSelectPosition(int position){
        selectPosition = position;
        if (adapter!=null){
            adapter.notifyDataSetChanged();
        }
    }

    public ItemsPopupWindow(Context context, ArrayList<String> arrayList, final AdapterView.OnItemClickListener onItemClickListener) {
        super(context);
        mContext = context;
        this.arrayList = arrayList;
        this.onItemClickListener = onItemClickListener;
        view = LayoutInflater.from(mContext).inflate(R.layout.layout_common_pop, null);
        listview = (ListView) view.findViewById(R.id.listview);
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(view);
        this.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        adapter = new PopAdapter();
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                setSelectPosition(position);
                dismiss();
                onItemClickListener.onItemClick(parent, view, position, id);
            }
        });
    }

    /**
     * 设置弹框的宽度
     *
     * @param dps
     */
    public ItemsPopupWindow setWidthDps(int dps) {
        this.setWidth(CommonUtil.dip2px(dps));
        return this;
    }

    public class PopAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return arrayList.size();
        }

        @Override
        public Object getItem(int position) {
            return arrayList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_pop_textview, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tv.setText(arrayList.get(position));
            /*if (selectPosition == position){
                holder.tv.setTextColor(mContext.getResources().getColor(R.color.grey_999));
            }else {
                holder.tv.setTextColor(mContext.getResources().getColor(R.color.black_19191a));
            }*/
            return convertView;
        }

        class ViewHolder {
            @Bind(R.id.tv_)
            TextView tv;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }


}
